<?php

class Beach{
	public $id;
	public $name;

	public function __construct($id, $name){
		$this->id = $id;
		$this->name = $name;
	}
	public function json_encode(){
		return object_to_array($this);
	}
}

function object_to_array($data){
	if(is_array($data) || is_object($data)){
		$result = array();
		foreach($data as $key => $value){ 
			$result[$key] = object_to_array($value); 
		}
		return $result;
	}
	return $data;
}

$pepe[] = new Beach(1, "Playa BCN 1");
$pepe[] = new Beach(2, "Playa BCN 2");

print_r($pepe);
echo "<br/><br/>";
echo "\n\n";

$array = $pepe->object_to_array();
print_r($array);

echo "<br/><br/>";
echo "\n\n";

$json = json_encode($array);
print_r($json);

?>